package cn.itcast.bassm.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import cn.itcast.bassm.service.QQMailService;
import cn.itcast.bassm.util.CreateSimpleExcelToDisk;
import cn.itcast.bassm.util.NullMailUtil;
import cn.itcast.bassm.util.ZPMailUtil;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

@Service
public class QQMailServiceImpl implements QQMailService{

	/**
	 * 调用白名单记录接口
	 * @param url 接口地址
	 * @throws Throwable 
	 * @throws Exception 
	 */
	public String Acquisitiontime(String url) throws Throwable {
				int TotalBai = Acquisitiontimetwo(url);
				Calendar curr2 = Calendar.getInstance();
	            curr2.set(Calendar.DATE,curr2.get(Calendar.DATE)-1);
	          	Date date2=curr2.getTime();  
	          	SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");  
	          	String validTo2= sdf3.format(date2);
				String typexls="BMD";
				String  dateFormat2 ="yyyy-MM-dd HH:mm:ss";
				String zhanghao="admin";
				String passwork="123456";
				String startTime=validTo2+" 00:00";
				String endTime=validTo2+" 23:59";				
				
				String type ="3";
				String size ="900000";
				
				
		        List<NameValuePair> params = new ArrayList<NameValuePair>();
		        params.add(new BasicNameValuePair("type", type));
		        params.add(new BasicNameValuePair("startTime", startTime));
		        params.add(new BasicNameValuePair("endTime", endTime));
		        params.add(new BasicNameValuePair("size", size));
		        
		        HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");// 设置字符集
		        HttpPost httpPost = new HttpPost(url);
		        httpPost.setEntity(entity);// 设置参数实体
		        httpPost.addHeader("Authorization", getHeader(zhanghao,passwork));
		        @SuppressWarnings("resource")
		        HttpClient httpClient = new DefaultHttpClient();// 获取HttpClient对象
		        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);// 连接超时
		        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);// 请求超时
		        String msg5=null;
		        HttpResponse httpResp;
				try {
					String result = null;
					httpResp = httpClient.execute(httpPost);
					String json = null;
					String age = null;
					String personName= null;
					String cameraName=null;
					String dbName=null;
					String  datelong;
					 if (httpResp.getStatusLine().getStatusCode() == 200) {// 判断是够请求成功
				          json = EntityUtils.toString(httpResp.getEntity(), "UTF-8");// 获取返回的数据  
				          JsonParser parser = new JsonParser(); // 创建JSON解析器
			              JsonObject object = (JsonObject) parser.parse(json); // 创建JsonObject对象
			              JsonObject dataobject = object.get("data").getAsJsonObject();
			              JsonArray array = dataobject.get("content").getAsJsonArray();
			            	  		int Total =array.size();
			            	  		msg5=ZPMailUtil.SenMailGotoo(Total, TotalBai);
				        } else {
				           System.out.println("出错了"+httpResp.getStatusLine().getStatusCode());
				        }
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				return msg5;
		       
			}
	

	public int  Acquisitiontimetwo(String url) throws Throwable {
			int bertwo=0;
			Calendar curr2 = Calendar.getInstance();
            curr2.set(Calendar.DATE,curr2.get(Calendar.DATE)-1);
          	Date date2=curr2.getTime();  
          	SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");  
          	String validTo2= sdf3.format(date2);
			String typexls="ZP";
			String  dateFormat2 ="yyyy-MM-dd HH:mm:ss";
			String zhanghao="admin";
			String passwork="123456";
			String startTime=validTo2+" 00:00";
			String endTime=validTo2+" 23:59";
			String type ="2";
			String size ="900000";
			
			
	        List<NameValuePair> params = new ArrayList<NameValuePair>();
	        params.add(new BasicNameValuePair("type", type));
	        params.add(new BasicNameValuePair("startTime", startTime));
	        params.add(new BasicNameValuePair("endTime", endTime));
	        params.add(new BasicNameValuePair("size", size));
	        
	        HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");// 设置字符集
	        HttpPost httpPost = new HttpPost(url);
	        httpPost.setEntity(entity);// 设置参数实体
	        httpPost.addHeader("Authorization", getHeader(zhanghao,passwork));
	        @SuppressWarnings("resource")
	        HttpClient httpClient = new DefaultHttpClient();// 获取HttpClient对象
	        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);// 连接超时
	        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);// 请求超时
	        String msg6=null;
	        HttpResponse httpResp;
			try {
				String result = null;
				httpResp = httpClient.execute(httpPost);
				String json = null;
				String age = null;
				String personName= null;
				String cameraName=null;
				String dbName=null;
				String  datelong;
				 if (httpResp.getStatusLine().getStatusCode() == 200) {// 判断是够请求成功
			          json = EntityUtils.toString(httpResp.getEntity(), "UTF-8");// 获取返回的数据    
			          JsonParser parser = new JsonParser(); // 创建JSON解析器
		              JsonObject object = (JsonObject) parser.parse(json); // 创建JsonObject对象
		              JsonObject dataobject = object.get("data").getAsJsonObject();
		              JsonArray array = dataobject.get("content").getAsJsonArray();
		              bertwo = array.size(); 
			        } else {
			           System.out.println("出错了"+httpResp.getStatusLine().getStatusCode());
			        }
			} catch (ClientProtocolException e) {

				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			return bertwo;
		}
	/**
     * 构造Basic Auth认证头信息
     * 
     * @return
     */
    private static String getHeader(String username, String password) {
        String auth = username + ":" + password;
        byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
        String authHeader = "Basic " + new String(encodedAuth);
        return authHeader;
    }
}
